package com.grow.greenhouse.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Properties;

@Component
public class MailUtil {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private JavaMailSenderImpl sender;

    @Value("${spring.mail.username}")
    private String from;

    private static Properties pro;

    /**
     * 发送文本邮件
     * @param to
     * @param subject
     * @param content
     */

    static {
        pro = System.getProperties(); // 下面各项缺一不可
        pro.put("mail.smtp.auth", "true");
        pro.put("mail.smtp.ssl.enable", "true");
        pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }

    public void sendSimpleMail(String to, String subject, String content) {

        sender.setJavaMailProperties(pro);

        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from); // 发送人
            helper.setTo(to); // 收件人
            helper.setSubject(subject); // 标题
            helper.setText(content); // 内容
            sender.send(message);
            logger.info("简单邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送简单邮件时发生异常！", e);
            e.printStackTrace();
        } catch (Exception e) {
            logger.error("发送简单邮件时发生异常！", e);
            e.printStackTrace();
        }
    }


    /**
     * 发送html邮件
     * @param to
     * @param subject
     */

    public void sendHtmlMail(String to, String subject,String veryNum) {


        // 设计HTML邮件内容，包含随机的6位数
        String content = "<html>\n" +
                "<body>\n" +
                "  Hello, your code is: <br/><h3 style='color:red'>" + veryNum + " </h3>\n" +
                "</body>\n" +
                "</html>";

        sender.setJavaMailProperties(pro);
        MimeMessage message = sender.createMimeMessage();
        try {
            // true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            // 第二个参数为true表示发送HTML邮件
            helper.setText(content, true);
            sender.send(message);
            logger.info("HTML邮件发送成功");
        } catch (MessagingException e) {
            logger.error("发送HTML邮件时发生异常！", e);
            throw new RuntimeException(e);
        }
    }


    /**
     * 发送带附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePath
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath){
        sender.setJavaMailProperties(pro);
        MimeMessage message = sender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            sender.send(message);
            logger.info("带附件的邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送带附件的邮件时发生异常！", e);
            throw new RuntimeException(e);
        }
    }


}
